package client;

import main.Principale;
import mysql.RequeteSql;

import com.trolltech.qt.core.QDate;
import com.trolltech.qt.gui.*;

public class InscriptionBox extends QWidget
{
/* Données dont un accès peut-être utile depuis le reste de
la classe */

	private QTextEdit addressLineEdit;
	private QLineEdit nomLineEdit,prenomLineEdit, numeroTelLineEdit;
	private QComboBox formuleComboBox, zoneComboBox;
	private QDateEdit dateDateEdit;
	private QPushButton butValider, butCancel, butAddZone, butOperationZone;
    private Principale parent;
	
	private void init()
	{
		/*setStyleSheet("background-clip:padding;\n"+
				" background-image:url( classpath:api/images/ciptrans.jpg);\n"+
				"background-repeat: no-repeat;\n"+
				"background-position: center;\n"+
				"background-color:white;\n"+
				"QLineEdit{background-color:white}");*/
		butAddZone = new QPushButton(tr("Ajout Zone"));
		butOperationZone = new QPushButton(tr("Operation Zone"));
		zoneComboBox = new QComboBox();
		nomLineEdit = new QLineEdit();
		prenomLineEdit = new QLineEdit();
		numeroTelLineEdit = new QLineEdit();
		
		addressLineEdit = new QTextEdit();
		dateDateEdit = new QDateEdit();
		formuleComboBox = new QComboBox();
		formuleComboBox.setFocus();
		
		dateDateEdit.setFocus();
		
		
		nomLineEdit.setFocus();
		prenomLineEdit.setFocus();
		numeroTelLineEdit.setFocus();
		addressLineEdit.setFocus();
	}
	
	
	public InscriptionBox(Principale parent)
	{
		super(parent);
		this.parent = parent;
		
		
		RequeteSql.TAP_InitialiserConnectionJDBC();
		init();
		/** Login's GroupBox' **/
		QGroupBox loginGroup = new QGroupBox(tr("Information d inscription:"));
		/* Login Line Edit with Label */
		QLabel nomLabel = new QLabel(tr("Nom:"));
		QLabel prenomLabel = new QLabel(tr("Prenom:"));
		QLabel numLabel = new QLabel(tr("Numero tel:"));
		QLabel addrLabel = new QLabel(tr("Addresse:"));
		QLabel formuleLabel = new QLabel(tr("Formule:"));
		QLabel zoneLabel = new QLabel(tr("Zone :"));
		QHBoxLayout zoneLayout = new QHBoxLayout();
		rempliZoneComboBox();
		zoneLayout.addWidget(zoneLabel);
		zoneLayout.addWidget(zoneComboBox);
		zoneLayout.addWidget(butAddZone);
		zoneLayout.addWidget(butOperationZone);
		
		
		rempliZoneComboBox();
		Object ob[][] = RequeteSql.clientSelectFormule();
		for (int i = 0; i < ob.length; i++)
		{
			formuleComboBox.addItem((String) ob[i][0]);
		}
		
		
		
		
		QLabel dateLabel = new QLabel(tr("Date inscription:"));
		
		/* Login's Layout */
		QGridLayout inscriptionLayout = new QGridLayout();
		
		inscriptionLayout.addWidget(nomLabel, 0, 0);
		inscriptionLayout.addWidget(nomLineEdit, 0, 1);
		
		inscriptionLayout.addWidget(prenomLabel, 1, 0);
		inscriptionLayout.addWidget(prenomLineEdit, 1, 1);
		
		inscriptionLayout.addWidget(numLabel, 2, 0);
		inscriptionLayout.addWidget(numeroTelLineEdit, 2, 1);
		
		inscriptionLayout.addWidget(formuleLabel, 3, 0);
		inscriptionLayout.addWidget(formuleComboBox, 3, 1);
		
		inscriptionLayout.addWidget(dateLabel, 4, 0);
		new QDate();
		dateDateEdit.setDate(QDate.currentDate());
		inscriptionLayout.addWidget(dateDateEdit, 4, 1);
		
		inscriptionLayout.addWidget(addrLabel, 5, 0);
		addressLineEdit.setFixedHeight(50);
		inscriptionLayout.addWidget(addressLineEdit, 5, 1);
		
		inscriptionLayout.addLayout(zoneLayout, 6, 0, 3, 3);
		
		
		
		loginGroup.setLayout(inscriptionLayout);
		/** Account GroupBox **/
		/* Dialog Button Box */
		QHBoxLayout butLayout = new QHBoxLayout();
		butValider = new QPushButton("Valider");
		butCancel = new QPushButton("Annuler");
		butValider.setFixedSize(75, 25);
		butCancel.setFixedSize(75, 25);
		butCancel.clicked.connect(this, "slotClose()");
		butValider.clicked.connect(this, "slotBoutonValider()");
		butAddZone.clicked.connect(this, "slotAddZoneDial()");
		butLayout.addWidget(butValider);
		butLayout.addWidget(butCancel);
		
		QGroupBox butGroup = new QGroupBox();
		butGroup.setLayout(butLayout);
		//QDialogButtonBox boutons = new QDialogButtonBox();
		
		//boutons.addButton(QDialogButtonBox.StandardButton.Ok);
		//boutons.addButton(QDialogButtonBox.StandardButton.Cancel);
		
		// Dialog Layout
		QGridLayout layout = new QGridLayout();
		layout.addWidget(loginGroup, 0, 0);
		
		layout.addWidget(butGroup, 1, 0);
		setWindowTitle(tr("Inscription"));
		setLayout(layout);
	}
	
	

	public void slotClose()
	{
	  parent.ouvertureFenetreStatisciq();
	  RequeteSql.TAP_closeConnection();
	  close();
	}
	
	public void slotAddZoneDial()
	{
		QDialog dial = new DiallAddZone(/*this*/);
		dial.setModal(true);
		dial.show();
		dial.finished.connect(this, "test(int)");
		dial.accepted.connect(this, "test()");
		//QMessageBox.information(null, "bravo", "La zone a ete ajoute avec succes");
	}
	
	
	public void rempliZoneComboBox()
	{
		Object ob[][] = RequeteSql.zoneSelect();
		zoneComboBox.clear();
		for (int i = 0; i < ob.length; i++)
		{
			zoneComboBox.addItem((String) ob[i][1]);
			
		}
	}
	
	public void slotBoutonValider()
	{
		if ((nomLineEdit.text().isEmpty() == true ) || (prenomLineEdit.text().isEmpty() == true ) || (numeroTelLineEdit.text().isEmpty() == true)  || (dateDateEdit.text().isEmpty() == true) || (formuleComboBox.currentText().isEmpty() == true )) 
		{
			QMessageBox.information(null, "Erreur", "Veillez renseigner tous les champ SVP");
			System.out.println(formuleComboBox.currentText());
		}
		else
		{
			
			
			dateDateEdit.setDisplayFormat("yyyy/MM/dd");
			
		    boolean a =	RequeteSql.clientRemplieChamp(nomLineEdit.text(), prenomLineEdit.text(), numeroTelLineEdit.text(), formuleComboBox.currentText(), dateDateEdit.text(), addressLineEdit.toPlainText() , zoneComboBox.currentText());
			dateDateEdit.setDisplayFormat("dd/MM/yyyy");
			if (a != false)
			QMessageBox.information(null, "Bravo", "L utilisateur a ete inscrit avec succes");
			else
				QMessageBox.critical(null, "ERROR", "ERREUR BASE DE DONNE");
			RequeteSql.TAP_closeConnection();
			slotClose();

		}
	}
	
	
	
	public void test(int a)
	{
		rempliZoneComboBox();
		
	}
	
	public void test()
	{
		System.out.println("fenetre fermer \n code error = " );
	}
	
	/*public static void main(String args[])
	{
		QApplication.initialize(args);
		InscriptionBox widget = new InscriptionBox(); 
		widget.show();
		QApplication.exec();
	}*/
}